﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataAccess;
using Model.EntityModel;

namespace Project
{
    /// <summary>
    /// Interaction logic for StatisticalReportsContent.xaml
    /// </summary>
    public partial class StatisticalReportsContent : UserControl
    {
        private bool wasError;
        private List<Complaint> totalComplaints ;

        public StatisticalReportsContent()
        {
            InitializeComponent();
            try
            {
                Loaded += loaded;
                totalComplaints = ComplaintDataAccess.GetAllComplaints();
                lblNumberOfCases.Content = totalComplaints.Count;
                DateTime displayDateStart = totalComplaints[0].RegistrationDate;
                if (displayDateStart == DateTime.MinValue)
                    displayDateStart = DateTime.Now.AddYears(-10);
                dateStart.DisplayDateStart = displayDateStart;
                dateStart.DisplayDateEnd = DateTime.Now;
                dateStart2.DisplayDateStart = displayDateStart;
                dateStart2.DisplayDateEnd = DateTime.Now;
                dateEnd.DisplayDateStart = displayDateStart;
                dateEnd.DisplayDateEnd = DateTime.Now;
                dateEnd2.DisplayDateStart = displayDateStart;
                dateEnd2.DisplayDateEnd = DateTime.Now;
                dateStartTotalCases.DisplayDateStart = displayDateStart;
                dateStartTotalCases.DisplayDateEnd = DateTime.Now;
                dateEndTotalCases.DisplayDateStart = displayDateStart;
                dateEndTotalCases.DisplayDateEnd = DateTime.Now;
                var derivationCasesResult = ComplaintDataAccess.GetComplaintsDerivations(totalComplaints);
                ((BarSeries)mcChart.Series[0]).ItemsSource = derivationCasesResult;
                var violenceFormsResult = ComplaintDataAccess.GetComplaintsViolenceForms(totalComplaints);
                ((PieSeries)FactTypesChart.Series[0]).ItemsSource = violenceFormsResult;
                UIWindowManager.SetWindowTitle(this,"Brigada De Protección a la Familia - Generar Reportes Estadísticos");
            }
            catch (Exception)
            {
                wasError = true;
                Xceed.Wpf.Toolkit.MessageBox.Show(
                    "No se puede sacar reportes estadísticos porque no hay ninguna denuncia registrada!", "Información!",
                    MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void loaded(object sender, RoutedEventArgs e)
        {
            if(wasError)
                UIWindowManager.CleanWindow(this);
        }

        private List<Complaint> FilterListByDates(List<Complaint> complaints, Nullable<DateTime> startDate, Nullable<DateTime> endDate)
        {
            List<Complaint> res = new List<Complaint>();
            if (startDate == null)
                startDate = DateTime.MinValue;
            if (endDate == null)
                endDate = DateTime.Now;
            res = (from comp in complaints
                   where comp.RegistrationDate<=endDate && comp.RegistrationDate>=startDate
                   select comp).ToList();
            return res;
        }

        private void btnResetDates1_Click(object sender, RoutedEventArgs e)
        {
            dateStart.SelectedDate = null;
            dateStart.DisplayDate = DateTime.MinValue;
            dateEnd.SelectedDate = null;
            dateEnd.DisplayDateEnd = DateTime.MinValue;
        }

        private void dateStart_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            dateEnd.DisplayDateStart = dateStart.SelectedDate;
            ((BarSeries) mcChart.Series[0]).ItemsSource =
                ComplaintDataAccess.GetComplaintsDerivations(FilterListByDates(totalComplaints, dateStart.SelectedDate,
                                                                               dateEnd.SelectedDate));
        }

        private void dateEnd_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            dateStart.DisplayDateEnd = dateEnd.SelectedDate;
            ((BarSeries)mcChart.Series[0]).ItemsSource =
                ComplaintDataAccess.GetComplaintsDerivations(FilterListByDates(totalComplaints, dateStart.SelectedDate,
                                                                               dateEnd.SelectedDate));
        }

        private void btnResetDates2_Click(object sender, RoutedEventArgs e)
        {
            dateStart2.SelectedDate = null;
            dateStart2.DisplayDate = DateTime.MinValue;
            dateEnd2.SelectedDate = null;
            dateEnd2.DisplayDateEnd = DateTime.MinValue;
        }

        private void dateStart2_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            dateEnd2.DisplayDateStart = dateStart2.SelectedDate;
            ((PieSeries)FactTypesChart.Series[0]).ItemsSource =
                ComplaintDataAccess.GetComplaintsDerivations(FilterListByDates(totalComplaints, dateStart2.SelectedDate,
                                                                               dateEnd2.SelectedDate));
        }

        private void dateEnd2_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            dateStart2.DisplayDateEnd = dateEnd2.SelectedDate;
            ((PieSeries)FactTypesChart.Series[0]).ItemsSource =
                ComplaintDataAccess.GetComplaintsDerivations(FilterListByDates(totalComplaints, dateStart2.SelectedDate,
                                                                               dateEnd2.SelectedDate));
        }

        private void btnResetTotalCasesDates_Click(object sender, RoutedEventArgs e)
        {
            dateStartTotalCases.SelectedDate = null;
            dateStartTotalCases.DisplayDate = DateTime.MinValue;
            dateEndTotalCases.SelectedDate = null;
            dateEndTotalCases.DisplayDateEnd = DateTime.MinValue;
        }

        private void dateStartTotalCases_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            dateEndTotalCases.DisplayDateStart = dateStartTotalCases.SelectedDate;
            lblNumberOfCases.Content = FilterListByDates(totalComplaints,dateStartTotalCases.SelectedDate,dateEndTotalCases.SelectedDate).Count;
        }

        private void dateEndTotalCases_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            dateStartTotalCases.DisplayDateEnd = dateEndTotalCases.SelectedDate;
            lblNumberOfCases.Content = FilterListByDates(totalComplaints, dateStartTotalCases.SelectedDate, dateEndTotalCases.SelectedDate).Count;
        }
    }
}
